package com.eeepay.eeepay_v2.activity;

import android.text.TextUtils;

import com.eeepay.eeepay_v2.R;
import com.eeepay.eeepay_v2.util.BaseGrpcTask;
import com.eeepay.eeepay_v2.util.Constant;
import com.eeepay.v2_library.log.LogUtils;
import com.eeepay.v2_library.ui.ABBaseActivity;
import com.eeepay.v2_library.view.LeftRightText;

import java.util.concurrent.TimeUnit;

import cn.eeepay.api.grpc.nano.AgentApiServiceGrpc;
import cn.eeepay.api.grpc.nano.AgentSerProto;
import cn.eeepay.api.grpc.nano.ReqBaseProto;
import io.grpc.ManagedChannel;

/**
 * Created by zw on 2016/3/14 0014.
 * 代理商基本信息
 */
public class AgentBaseInfoActivity extends ABBaseActivity{

    //基本信息
    private LeftRightText agentName, peopleName, agentAddress, phone,
                            email, superAgent, level, status;
    //结算信息
    private LeftRightText accountType,//账户类型
                          openName,//开户名
                          openNum,//开户账号
                          bankName,//银行全称
                          bankNum;//联行行号

    private String agentId;//代理商Id

    private GrpcTask grpcTask;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_agent_base_info;
    }

    @Override
    protected void initWidget() {
        agentName = getViewById(R.id.lrv_agent_name);
        peopleName = getViewById(R.id.lrv_people_name);
        agentAddress = getViewById(R.id.lrv_address);
        phone = getViewById(R.id.lrv_phone);
        email = getViewById(R.id.lrv_email);
        superAgent = getViewById(R.id.lrv_super_agent);
        level = getViewById(R.id.lrv_level);
        status = getViewById(R.id.lrv_state);

        accountType = getViewById(R.id.lrv_account_type);
        openName = getViewById(R.id.lrv_open_name);
        openNum = getViewById(R.id.lrv_open_number);
        bankName = getViewById(R.id.lrv_bank_name);
        bankNum = getViewById(R.id.lrv_bank_number);

        if (bundle != null) {
            agentId = bundle.getString(Constant.AGENT_ID);
            LogUtils.d("TAG", "agent_id = " + agentId);
        }

        grpcTask = new GrpcTask();
        sendHttpRequest(0);
    }

    @Override
    protected void sendHttpRequest(int taskId) {
        super.sendHttpRequest(taskId);
        showProgressDialog();
        grpcTask.execute(Constant.URL.URL, Constant.URL.PORT);
    }

    @Override
    protected void eventOnClick() {

    }

    String statuStr = "";
    String accType = "";

    private class GrpcTask extends BaseGrpcTask<AgentSerProto.AgentInfoApi> {

        @Override
        protected AgentSerProto.AgentInfoApi doInBack(ManagedChannel channel) {
            AgentApiServiceGrpc.AgentApiServiceBlockingStub stub = AgentApiServiceGrpc
                    .newBlockingStub(channel)
                    .withDeadlineAfter(5, TimeUnit.SECONDS);
            ReqBaseProto.ReqOneStr request = new ReqBaseProto.ReqOneStr();
            request.param = agentId;
            AgentSerProto.AgentInfoApi info = stub.queryAgentInfoByNo(request);
            return info;
        }

        @Override
        protected void updateUI(AgentSerProto.AgentInfoApi result) {
            dismissProgressDialog();
            if (result == null) {
                showToast("查询失败");
                return;
            }
            if ("false".equals(result.status)) {
                showToast("查询失败");
                return;
            }
            LogUtils.d("TAG", result.toString());
            agentName.setRightText(result.agentName);
            peopleName.setRightText(result.linkName);
            agentAddress.setRightText(result.agentArea);
            phone.setRightText(result.mobilephone);
            email.setRightText(result.email);
            superAgent.setRightText(result.parentId);
            level.setRightText(result.agentLevel + "级");
            //代理商状态 1：正常，0：关闭进件，2：冻结
            switch (result.status) {
                case "0":
                    statuStr = "关闭进件";
                    break;
                case "1":
                    statuStr = "正常";
                    break;
                case "2":
                    statuStr = "冻结";
                    break;
            }
            status.setRightText(statuStr);
            //账户类型  1：对公，2：对私
            switch (result.accountType) {
                case "1":
                    accType = "对公";
                    break;
                case "2":
                    accType = "对私";
                    break;
            }
            accountType.setRightText(accType);
            openName.setRightText(result.accountName);
            String behindNo = result.accountNo;
            if (!TextUtils.isEmpty(result.accountNo)) {
                behindNo = "************" + behindNo.substring(behindNo.length() - 4);
                openNum.setRightText(behindNo);
            }
            bankName.setRightText(result.bankName);
            bankNum.setRightText(result.cnapsNo);
        }
    }
}
